//
//  ZLSessionViewController.m
//  ZLYoYoIM_Example
//
//  Created by 赵磊 on 2021/1/15.
//  Copyright © 2021 itzhaolei. All rights reserved.
//

#import "ZLSessionViewController.h"
#import "ZLSessionConfig.h"
#import <AudioToolbox/AudioToolbox.h>
#import "ZLNavigationBarHeader.h"
#import <NIMKit/NIMKitUtil.h>
#import "UIImage+ZLIMCustom.h"
#import "ZLAttachment.h"
#import "ZLBurnAfterReadingCellContentView.h"
#import "ZLSessionCustomTypeHeader.h"

@interface ZLSessionViewController ()

@property (nonatomic,strong) ZLSessionConfig *config;
///导航条
@property (nonatomic,weak) ZLNavigationBar *navBar;

@end

@implementation ZLSessionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configTableView];
    [self registerAction];
    self.navBar.actionBar.rightMainItem.hidden = ![self isShowRightItem];
}

- (id<NIMSessionConfig>)sessionConfig{
    return self.config;
}
- (instancetype)initWithSession:(NIMSession *)session {
    if (self = [super initWithSession:session]) {
        _config = [[ZLSessionConfig alloc] init];
    }
    return self;
}
- (NIMAdvanceMenu *)advanceMenu
{
    return nil;
}


#pragma mark - Lazy
- (ZLNavigationBar *)navBar {
    if (!_navBar) {
        ZLNavigationBar *navBar = [ZLNavigationBar navigationBar];
        navBar.title = [NIMKitUtil showNick:self.session.sessionId inSession:self.session];;
        [navBar.actionBar.rightMainItem setImage:[UIImage imageNamedInIM:@"三个黑点"] forState:UIControlStateNormal];
        [self.view addSubview:navBar];
        _navBar = navBar;
    }
    return _navBar;
}

#pragma mark - Action
- (void)configTableView {
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.separatorStyle = NO;
    //ios11 适配
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
        self.tableView.estimatedRowHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;
    }
    BOOL isBangDevice = NO;
    if (@available(iOS 11.0, *)) {
        isBangDevice = UIApplication.sharedApplication.delegate.window.safeAreaInsets.bottom;
    }
    CGFloat ZL_Navigation_Height = isBangDevice ? 84.0 : 64.0;
    CGFloat homeBarHeight = isBangDevice ? 30.0 : 0.01;
    self.tableView.contentInset = UIEdgeInsetsMake(ZL_Navigation_Height, 0, homeBarHeight, 0);
}
- (void)registerAction {
    __weak typeof(self)weakSelf = self;
    self.navBar.actionBar.leftItem.click = ^{
        if (weakSelf.navigationController.viewControllers.firstObject == weakSelf) {
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
            return;
        }
        [weakSelf.navigationController popViewControllerAnimated:YES];
    };
    self.navBar.actionBar.rightMainItem.click = ^{
        [weakSelf clickRightItem];
    };
}

#pragma mark - Private
- (void)sendCustomMessage:(ZLAttachment *)attachment{
    
    //构造自定义MessageObject
    NIMCustomObject *object = [[NIMCustomObject alloc] init];
    object.attachment = attachment;
    
    //构造自定义消息
    NIMMessage *message = [[NIMMessage alloc] init];
    message.messageObject = object;
    
    //发送消息
    BOOL isWin = [[NIMSDK sharedSDK].chatManager sendMessage:message toSession:self.session error:nil];
    NSLog(@"发送%@",isWin ? @"成功" : @"失败");
}

#pragma mark - Overload
- (BOOL)onTapAvatar:(NIMMessage *)message {
    [self clickIconWithIMID:message.from MySelf:message.isOutgoingMsg];
    return YES;
} 

#pragma mark - WaitingCall
- (void)clickIconWithIMID:(NSString *)IMID MySelf:(BOOL)isMySelf {
    NSLog(@"点击头像已响应，被点击的IMID为:%@------%@",IMID,isMySelf ? @"自己发送的消息" : @"对方发来的消息");
}
- (void)clickRightItem {
    NSLog(@"右上角按钮已响应");
}
- (BOOL)isShowRightItem {
    return NO;
}
- (void)onTapBurnAfterReading {
    NSLog(@"点击阅后即焚");
}
- (void)onTapGiftGiving {
    NSLog(@"点击送礼物");
}
- (void)onTapVoiceCalls {
    NSLog(@"点击语音通话");
}
- (void)sendBurnAfterReadingWithUrlString:(NSString *)urlString {
    ZLAttachment *attachment = [[ZLAttachment alloc] init];
    attachment.url = urlString;
    attachment.type = [NSString stringWithFormat:@"%ld",ZLSessionCustomTypeBurnAfterReading];
    [self sendCustomMessage:attachment];
}
- (void)sendGiftWithUrlString:(NSString *)urlString {
    ZLAttachment *attachment = [[ZLAttachment alloc] init];
    attachment.url = urlString;
    attachment.type = [NSString stringWithFormat:@"%ld",ZLSessionCustomTypeGift];
    [self sendCustomMessage:attachment];
}

- (BOOL)onLongPressCell:(NIMMessage *)message
                 inView:(UIView *)view {
    AudioServicesPlaySystemSoundWithCompletion(kSystemSoundID_Vibrate, ^{});
    if ([view isKindOfClass:[ZLBurnAfterReadingCellContentView class]]) {
        
    }
    return YES;
}



@end
